package com.e.library.http;

import android.text.TextUtils;

import com.e.library.utils.EGsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxy on 2016/5/9.
 */
public class EResponse {

    private String json;
    private int status;
    private String message;
    private String session;
    private JSONObject obj;
    private JSONObject dataObj;

    public EResponse(String json){
        this.json = json;
        try {
            obj = new JSONObject(json);
            if (obj == null){
                return;
            }
            dataObj =obj.optJSONObject("data");
            message = obj.optString("message","");
            status = obj.optInt("status",-1);

            if (dataObj !=null){
                session = dataObj.optString("session_id","");
            }

            if (!isSuccess() && TextUtils.isEmpty(message)){

                /**
                 * 欠费情况下，将会使用网易的json
                 * {
                 "code":730,
                 "msg":"欠费或试用到期, 直播服务已关闭, 请联系客服恢复。"
                 }
                 */
                if (obj.has("msg")){
                    message = obj.getString("msg");
                }else {
                    message = EClient.status.get(status);
                }
            }


        } catch (JSONException e) {
            e.printStackTrace();
            message = EClient.status.get(-1);
            return;
        }
    }

    public boolean isSuccess(){
        return status == 200000;
    }

    public boolean isExpire(){
        return status == 400009;
    }

    public <T> T toObject(String key,Class<T> clazz){
        if (dataObj == null){
            return null;
        }
        String value = dataObj.optString(key,"");
        return EGsonUtils.toObject(value,clazz);
    }

    public <T> List<T> toList(String key,Class<T> clazz){
        if (dataObj == null){
            return new ArrayList<T>(0);
        }
        String value = dataObj.optString(key,"");
        return EGsonUtils.toList(value,clazz);
    }

    public String optString(String key,String defValue){
        if (dataObj == null){
            return defValue;
        }
        return dataObj.optString(key,defValue);
    }

    public int optInt(String key,int defValue){
        if (dataObj == null){
            return defValue;
        }
        return dataObj.optInt(key,defValue);
    }

    public double optDouble(String key,double defValue){
        if (dataObj == null){
            return defValue;
        }
        return dataObj.optDouble(key,defValue);
    }

    public long optLong(String key,long defValue){
        if (dataObj == null){
            return defValue;
        }
        return dataObj.optLong(key,defValue);
    }

    public boolean optBoolean(String key,boolean defValue){
        if (dataObj == null){
            return defValue;
        }
        return dataObj.optBoolean(key,defValue);
    }

    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public String getJson() {
        return json;
    }

    public JSONObject getJSONObject() {
        return obj;
    }

    public String getSession() {
        return session;
    }

    public JSONObject getDataObj() {
        return dataObj;
    }
}
